package com.gouaixin.bee.beeframework2.model;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.bean.getChatFriendsResponse;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.TransformUtils;
import com.gouaixin.bee.beeframework2.tool.XHttpUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/4/21.
 */
//聊天好友控制器
public class ChatFriendModel {

    /**
     * 获取聊天好友列表
     */
    public static void getChatFriends(Context context) {
        String url = "http://jiekou.hydgyx.com/api/interface_chatfriends.php";
        String cid = PreferencesUtils.getString(context, "cid");
        RequestParams params = new RequestParams();
        params.addBodyParameter("cid", cid);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST,
                url,
                params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        mLog.e(current + "/" + total);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        mLog.e("获取聊天好友列表：响应=" + TransformUtils.Unicode2Chinese(responseInfo.result));
                        String result = XHttpUtils.Unicode2Chinese(responseInfo.result);
                        onGetChatFriends(result);
                    }

                    @Override
                    public void onStart() {
                        mLog.e("开始请求：");
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        mLog.e("请求故障：" + msg);
                    }
                }
        );


    }


    /**
     * 获取聊天好友列表-响应值处理
     *
     * @param json
     */
    public static void onGetChatFriends(String json) {
        ArrayList<getChatFriendsResponse> datas = new ArrayList<getChatFriendsResponse>();
        if (!json.equals("")) {
            datas = new Gson().fromJson(json, new TypeToken<ArrayList<getChatFriendsResponse>>() {
            }.getType());

            if (!datas.isEmpty()) {
                if(mLister!=null){
                    mLister.onSuccess(datas);
                }
            }
        }

    }



    //自定义一个回调方法

    /**
     * 定义一个接口
     */
    public interface GetChatFriendLister {
        public void onSuccess(ArrayList<getChatFriendsResponse> datas);
    }

    /**
     * 初始化接口变量
     */
    private static GetChatFriendLister mLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setGetChatFriendLister(GetChatFriendLister mLister2) {
        mLister = mLister2;
    }



}
